package com.software.mty.shenhao;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.design.widget.BottomNavigationView;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.view.MenuItem;
import android.widget.TextView;

import fragment.DashboardFragment;
import fragment.DefaultFragment;
import okhttp3.OkHttpClient;

public class Home extends AppCompatActivity {

    private TextView mTextMessage;
    private OkHttpClient client;

    private FragmentManager fragmentManager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_home);

        mTextMessage = findViewById(R.id.message);
        BottomNavigationView navigation = findViewById(R.id.navigation);
        navigation.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener);

        fragmentManager = getSupportFragmentManager();
        FragmentTransaction transaction = fragmentManager.beginTransaction();
        transaction.add(R.id.fragment, new DefaultFragment());
        transaction.commit();
    }


    private BottomNavigationView.OnNavigationItemSelectedListener mOnNavigationItemSelectedListener
            = new BottomNavigationView.OnNavigationItemSelectedListener() {

        @Override
        public boolean onNavigationItemSelected(@NonNull MenuItem item) {
            FragmentTransaction transaction = fragmentManager.beginTransaction();
            switch (item.getItemId()) {
                case R.id.navigation_home:

                    transaction.replace(R.id.fragment,new DefaultFragment());
                    transaction.commit();
                    return true;
                case R.id.navigation_dashboard:
                    transaction.replace(R.id.fragment,new DashboardFragment());
                    transaction.commit();
//                    try {
//                        Request request = new Request.Builder().url("http://47.104.29.114:5000/temperature/getall").build();
//                        Response response = client.newCall(request).execute();
//                        Log.i("", request.body().toString());
//                    } catch (Exception e) {
//                        e.printStackTrace();
//                    }
                    return true;
                case R.id.navigation_notifications:
                    Intent intent = new Intent(Home.this,Mycenter.class);
                    startActivity(intent);
                    return true;
            }
            return false;
        }
    };
}
